home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / lib / dde.pl < prev    next >
Encoding:
Text File  |  1996-06-05  |  4.8 KB  |  159 lines

  1. /*  $Id: dde.pl,v 1.5 1996/06/05 14:57:22 jan Exp $
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     jan@swi.psy.uva.nl
  5.  
  6.     Purpose: MS-Windows DDE server and client.
  7. */
  8.  
  9. :- module(win_dde,
  10.       [ dde_request/3,        % +Handle, +Key, -Value
  11.         dde_execute/2,        % +Handle, +Command
  12.         dde_poke/3,            % +Handle, +Item, +Data
  13.         dde_register_service/2,    % +Template, +Goal
  14.         dde_unregister_service/1,    % +Service
  15.         dde_current_service/2,    % -Service, -Topic
  16.         dde_current_connection/2    % -Service, -Topic
  17.       ]).
  18.  
  19.          /*******************************
  20.          *          CLIENT        *
  21.          *******************************/
  22.  
  23. %    Make a DDE request with default timeout value (meaning the request
  24. %    will block VERY long).
  25.  
  26. dde_request(Handle, Key, Value) :-
  27.     dde_request(Handle, Key, Value, 0).
  28.  
  29. %    Make a DDE execute request with default timeout value.
  30.  
  31. dde_execute(Handle, Command) :-
  32.     dde_execute(Handle, Command, 0).
  33.  
  34. %    Make a DDE poke request with default timeout value.
  35.  
  36. dde_poke(Handle, Item, Data) :-
  37.     dde_poke(Handle, Item, Data, 0).
  38.  
  39.  
  40.          /*******************************
  41.          *          SERVER        *
  42.          *******************************/
  43.  
  44. :- dynamic
  45.     dde_service/6,
  46.     dde_current_connection/3.
  47.  
  48. :- module_transparent
  49.     dde_register_service/2.
  50.  
  51. %    dde_register_service(+Service(Topic, Item, Value), +Goal).
  52. %    dde_register_service(+Service(Topic, Command), +Goal).
  53. %
  54. %    Register a DDE service satisfied by this SWI-Prolog instance.  The
  55. %    first form is for dde_request/3, the second for dde_execute/2.
  56. %
  57. %    Topic is either an atom or a variable.  In the latter case, any
  58. %    topic is confirmed on the Service.  Item, Value and Command are
  59. %    variables used to pass the arguments into the Goal.
  60.  
  61. dde_register_service(Template, Goal) :-
  62.     Template =.. [Service, Topic, Item, Value], !,
  63.     '$strip_module'(Goal, Module, PlainGoal),
  64.     '$dde_register_service'(Service, on),
  65.     asserta(win_dde:dde_service(Service, Topic, Item,
  66.                     Value, Module, PlainGoal)).
  67. dde_register_service(Template, Goal) :-
  68.     Template =.. [Service, Topic, Command], !,
  69.     '$strip_module'(Goal, Module, PlainGoal),
  70.     '$dde_register_service'(Service, on),
  71.     asserta(win_dde:dde_service(Service, Topic, -,
  72.                     Command, Module, PlainGoal)).
  73. dde_register_service(Template, _Goal) :-
  74.     '$warning'('dde_register_service/2: illegal template: ~w', [Template]),
  75.     fail.
  76.  
  77. dde_unregister_service(Service) :-
  78.     (   retract(dde_service(Service, _, _, _, _, _))
  79.     ->  '$dde_register_service'(Service, off)
  80.     ;   true
  81.     ),
  82.     retractall(dde_service(Service, _, _, _, _, _)).
  83.  
  84. dde_unregister_all_services :-
  85.     dde_current_service(Service, _),
  86.     dde_unregister_service(Service),
  87.     fail ; true.
  88.     
  89. :- at_halt(dde_unregister_all_services). % required by Windows!
  90.  
  91. %    dde_current_service(?Service, ?Topic)
  92. %
  93. %    Unifies Service and Topic with currently supported servers/topics
  94.  
  95. dde_current_service(Service, Topic) :-
  96.     dde_service(Service, Topic, _, _, _, _).
  97.  
  98. %    dde_current_connection(?Service, ?Topic)
  99. %    
  100. %    Unifies Service and Topic with the currently open server connections.
  101.  
  102. dde_current_connection(Service, Topic) :-
  103.     dde_current_connection(_, Service, Topic).
  104.  
  105.          /*******************************
  106.          *        CALL-BACKS        *
  107.          *******************************/
  108.  
  109. %    $dde_connect(+Service, +Topic, +IsSelf)
  110. %
  111. %    Called by the DDEML XTYP_CONNECT request.  IsSelf is 0 if the
  112. %    connection is requested by another Prolog, 1 otherwise.
  113.  
  114. '$dde_connect'(Service, Topic, _Self) :-
  115.     dde_service(Service, Topic, _, _, _, _).
  116.  
  117. %    $dde_connect_confirm(+Service, +Topic, +Handle)
  118. %
  119. %    Called by the DDEML XTYP_CONNECT_CONFIRM request.  Used to update
  120. %    our list of current conversations.
  121.  
  122. '$dde_connect_confirm'(Service, Topic, Handle) :-
  123.     asserta(dde_current_connection(Handle, Service, Topic)).
  124.  
  125. %    $dde_disconnect(+Handle)
  126. %
  127. %    Called by the DDEML XTYP_DISCONNECT request.  Used to update our
  128. %    list of current conversations.
  129.  
  130. '$dde_disconnect'(Handle) :-
  131.     retractall(dde_current_connection(Handle, _, _)).
  132.  
  133. %    $dde_request(+Handle, +Topic, +Item, -Answer)
  134. %
  135. %    Called by the DDEML XTYP_REQUEST request.  Answer should be unified
  136. %    with a Prolog object that can be translated into a textual value
  137. %    (atom, string, number or list-of-ascii-values).
  138.  
  139. '$dde_request'(Handle, Topic, Item, Answer) :-
  140.     dde_current_connection(Handle, Service, Topic),
  141.     dde_service(Service, Topic, Item, Value, Module, Goal), !,
  142.     Module:Goal,
  143.     Answer = Value.
  144. '$dde_request'(_Handle, Topic, _Item, _Answer) :-
  145.     '$warning'('DDE server: no registrations for topic ~w~n', [Topic]).
  146.  
  147. %    $dde_execute(+Handle, +Topic, +Command)
  148. %
  149. %    Called by the DDEML XTYP_EXECUTE request.  Command is a SWI-Prolog
  150. %    string object.  Use string_to_atom or string_to_list to convert it
  151. %    into a standard object.
  152.  
  153. '$dde_execute'(Handle, Topic, Command) :-
  154.     dde_current_connection(Handle, Service, Topic),
  155.     dde_service(Service, Topic, _, Command, Module, Goal), !,
  156.     Module:Goal.
  157. '$dde_execute'(_Handle, Topic, _Command) :-
  158.     '$warning'('DDE server: no registrations for topic ~w~n', [Topic]).
  159.